#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cctype>

using namespace std;

enum TokenType { NUMBER, OPERATOR };

struct Token {
    TokenType type;
    int value;
    char op;
};

class Calculator {
private:
    vector<Token> tokens;

    void tokenize(const string& expr) {
        tokens.clear();
        size_t pos = 0;
        bool expect_number = true;

        while (pos < expr.size()) {
            if (isspace(expr[pos])) {
                pos++;
                continue;
            }

            if (expr[pos] == '-' && expect_number) {
                // ��������
                size_t start = pos++;
                while (pos < expr.size() && isdigit(expr[pos])) pos++;
                int value = stoi(expr.substr(start, pos - start));
                tokens.push_back({ NUMBER, value, '\0' });
                expect_number = false;
                continue;
            }

            if (isdigit(expr[pos])) {
                size_t start = pos;
                while (pos < expr.size() && isdigit(expr[pos])) pos++;
                int value = stoi(expr.substr(start, pos - start));
                tokens.push_back({ NUMBER, value, '\0' });
                expect_number = false;
                continue;
            }

            if (expr[pos] == '+' || expr[pos] == '-' || expr[pos] == '*' || expr[pos] == '/') {
                tokens.push_back({ OPERATOR, 0, expr[pos] });
                pos++;
                expect_number = true;
                continue;
            }

            throw invalid_argument("�Ƿ��ַ�: " + string(1, expr[pos]));
        }
    }

    int evaluate() {
        // �ȴ����˳�
        vector<Token> processed;
        for (size_t i = 0; i < tokens.size(); ++i) {
            if (tokens[i].type == OPERATOR && (tokens[i].op == '*' || tokens[i].op == '/')) {
                int left = processed.back().value;
                processed.pop_back();
                i++; // ���������
                int right = tokens[i].value;
                int result = (tokens[i - 1].op == '*') ? left * right : left / right;
                processed.push_back({ NUMBER, result, '\0' });
            }
            else {
                processed.push_back(tokens[i]);
            }
        }

        // �ٴ����Ӽ�
        int result = processed[0].value;
        for (size_t i = 1; i < processed.size(); ++i) {
            if (processed[i].type == OPERATOR) {
                int operand = processed[++i].value;
                switch (processed[i - 1].op) {
                case '+': result += operand; break;
                case '-': result -= operand; break;
                }
            }
        }
        return result;
    }

public:
    int calculate(const string& expr) {
        tokenize(expr);
        if (tokens.empty()) throw invalid_argument("�ձ���ʽ");
        return evaluate();
    }
};

int main() {
    Calculator calc;
    string input;

    while (true) {
        cout << "Calculator> ";
        if (!getline(cin, input)) break;
        if (input.empty()) continue;

        try {
            cout << calc.calculate(input) << endl;
        }
        catch (const exception& e) {
            cerr << "����: " << e.what() << endl;
        }
    }

    return 0;
}